//
//  MNDMonthlyTransactionViewController.m
//  minidai
//
//  Created by Steven.Yung on 2017/5/17.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDMonthlyTransactionViewController.h"
#import "SYCircleProgressView.h"
#import "MNDMonthlyTransactionModel.h"
#import "HooDatePicker.h"
#import "NSDate+JKExtension.h"
#import "NSDate+JKUtilities.h"

#define kProgressColor [UIColor colorWithRed:255/255.0 green:110/255.0 blue:63/255.0 alpha:1.0]

@interface MNDMonthlyTransactionViewController () <HooDatePickerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *lineGapImage;
/*! 当前选择的日期 默认是本月 */
@property (weak, nonatomic) IBOutlet UILabel *selectedDateLabel;

@property (weak, nonatomic) IBOutlet SYCircleProgressView *circleProgressView;
///*! 资产总额 */
//@property (weak, nonatomic) IBOutlet UILabel *assetAmountLabel;
/*! 投资总额 */
@property (weak, nonatomic) IBOutlet UILabel *tenderAmountLabel;
/*! 回款总额 */
@property (weak, nonatomic) IBOutlet UILabel *returnAmountLabel;
/*! 收益总额 */
@property (weak, nonatomic) IBOutlet UILabel *profitAmountLabel;
/*! 充值总额 */
@property (weak, nonatomic) IBOutlet UILabel *rechargeAmountLabel;
/*! 提现总额 */
@property (weak, nonatomic) IBOutlet UILabel *withdrawAmountLabel;
@property (weak, nonatomic) IBOutlet UIImageView *leftGapImage;
@property (weak, nonatomic) IBOutlet UIImageView *rightGapImage;

@property (strong, nonatomic) MNDMonthlyTransactionModel *monthlyModel;


@end

@implementation MNDMonthlyTransactionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.lineGapImage setImage:[[UIImage imageNamed:@"month_gap"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 1)]];
    self.leftGapImage.image = [[UIImage imageNamed:@"investment_record_gap"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 1)];
    self.rightGapImage.image = [[UIImage imageNamed:@"investment_record_gap"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 1, 0, 1)];
    self.selectedDateLabel.text = [NSDate jk_stringWithDate:self.chooseDate format:@"yyyy年MM月"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadDatas];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.circleProgressView progressPercent:100.0 withColor: kProgressColor animated:YES];
}

- (void)loadDatas {
    
    WeakSelf(self);
    [NetworkProgressHUD showHud];
    
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"y": @(self.chooseDate.jk_year), @"m": @(self.chooseDate.jk_month)}];
    [APIManager sendRequestWithPath:API_USER_TRANSACTION_COUNT parameters:params method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            weakself.monthlyModel = [MNDMonthlyTransactionModel mj_objectWithKeyValues:responseModel.data];
            
            [weakself configDatas];
            
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
                
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (void)configDatas {

    self.selectedDateLabel.text = [NSDate jk_stringWithDate:self.chooseDate format:@"yyyy年MM月"];
    
    self.tenderAmountLabel.text = [self filterSymbol:@"-" atString:self.monthlyModel.user_tender_amount];
    self.returnAmountLabel.text = [self filterSymbol:@"-" atString:self.monthlyModel.user_return_total];
    self.profitAmountLabel.text = [self filterSymbol:@"-" atString:self.monthlyModel.return_actual_profit];
    self.rechargeAmountLabel.text = [self filterSymbol:@"-" atString:self.monthlyModel.user_amount_recharge];
    self.withdrawAmountLabel.text = [self filterSymbol:@"-" atString:self.monthlyModel.user_amount_withdraw];
}

- (NSString *)filterSymbol:(NSString *)symbol atString:(NSString *)string {
    if ([string containsString:symbol]) {
        
        NSRange range = [string rangeOfString:symbol];
        NSString *filterStr = [string substringFromIndex:range.location + 1];
        return filterStr;
        
    } else {
        return string;
    }
}

- (IBAction)chooseDateAction:(id)sender {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *minDate = [formatter dateFromString:@"2013-02-01"];
    
    
    HooDatePicker *picker = [[HooDatePicker alloc] initDatePickerMode:HooDatePickerModeYearAndMonth
                                                              minDate: minDate
                                                           maxMamDate: [NSDate date]
                                                    andAddToSuperView: self.view];
    picker.hideSelectedLine = YES;
    picker.delegate = self;
    
    [picker show];
}

#pragma mark - HooDatePicker Delegate

- (void)datePicker:(HooDatePicker *)datePicker dateDidChange:(NSDate *)date {
    NSLog(@"date: %@", date);
}

- (void)datePicker:(HooDatePicker *)dataPicker didSelectedDate:(NSDate *)date {
    NSLog(@"selected date: %@", date);
    //所选日期与当前显示日期相同，不做请求处理
    if ([self.chooseDate jk_isSameYearAsDate:date] && [self.chooseDate jk_isSameMonthAsDate:date]) {
        return;
    }
    self.chooseDate = date;
    [self loadDatas];
}

- (void)datePicker:(HooDatePicker *)datePicker didCancel:(UIButton *)sender {
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
